home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / bbs / fcfgw40s.zip / PROGDLG.CPP < prev    next >
C/C++ Source or Header  |  1996-04-22  |  5KB  |  203 lines

  1. //  ProgDlg.cpp : implementation file
  2. // CG: This file was added by the Progress Dialog component
  3.  
  4. #include "stdafx.h"
  5. #include "resource.h"
  6. #include "ProgDlg.h"
  7.  
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char BASED_CODE THIS_FILE[] = __FILE__;
  11. #endif
  12.  
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CProgressDlg dialog
  15.  
  16. CProgressDlg::CProgressDlg(UINT nCaptionID)
  17. {
  18.     m_nCaptionID = CG_IDS_PROGRESS_CAPTION;
  19.     if (nCaptionID != 0)
  20.         m_nCaptionID = nCaptionID;
  21.  
  22.     m_nLower=0;
  23.     m_nUpper=100;
  24.     m_nStep=10;
  25.     //{{AFX_DATA_INIT(CProgressDlg)
  26.     //}}AFX_DATA_INIT
  27.     m_bParentDisabled = FALSE;
  28. }
  29.  
  30. CProgressDlg::~CProgressDlg()
  31. {
  32.     if(m_hWnd!=NULL)
  33.       DestroyWindow();
  34. }
  35.  
  36. BOOL CProgressDlg::DestroyWindow()
  37. {
  38.     ReEnableParent();
  39.     return CDialog::DestroyWindow();
  40. }
  41.  
  42. void CProgressDlg::ReEnableParent()
  43. {
  44.     if(m_bParentDisabled && (m_pParentWnd!=NULL))
  45.       m_pParentWnd->EnableWindow(TRUE);
  46.     m_bParentDisabled=FALSE;
  47. }
  48.  
  49. BOOL CProgressDlg::Create(CWnd *pParent)
  50. {
  51.     // Get the true parent of the dialog
  52.     m_pParentWnd = CWnd::GetSafeOwner(pParent);
  53.  
  54.     // m_bParentDisabled is used to re-enable the parent window
  55.     // when the dialog is destroyed. So we don't want to set
  56.     // it to TRUE unless the parent was already enabled.
  57.  
  58.     if((m_pParentWnd!=NULL) && m_pParentWnd->IsWindowEnabled())
  59.     {
  60.       m_pParentWnd->EnableWindow(FALSE);
  61.       m_bParentDisabled = TRUE;
  62.     }
  63.  
  64.     if(!CDialog::Create(CProgressDlg::IDD,pParent))
  65.     {
  66.       ReEnableParent();
  67.       return FALSE;
  68.     }
  69.  
  70.     return TRUE;
  71. }
  72.  
  73. void CProgressDlg::DoDataExchange(CDataExchange* pDX)
  74. {
  75.     CDialog::DoDataExchange(pDX);
  76.     //{{AFX_DATA_MAP(CProgressDlg)
  77.     DDX_Control(pDX, IDC_ANIMATE_PROGRESS, m_Animation);
  78.     DDX_Control(pDX, CG_IDC_PROGDLG_PROGRESS, m_Progress);
  79.     //}}AFX_DATA_MAP
  80. }
  81.  
  82. BEGIN_MESSAGE_MAP(CProgressDlg, CDialog)
  83.     //{{AFX_MSG_MAP(CProgressDlg)
  84.     //}}AFX_MSG_MAP
  85. END_MESSAGE_MAP()
  86.  
  87. void CProgressDlg::SetStatus(LPCTSTR lpszMessage)
  88. {
  89.     ASSERT(m_hWnd); // Don't call this _before_ the dialog has
  90.                     // been created. Can be called from OnInitDialog
  91.     CWnd *pWndStatus = GetDlgItem(CG_IDC_PROGDLG_STATUS);
  92.  
  93.     // Verify that the static text control exists
  94.     ASSERT(pWndStatus!=NULL);
  95.     pWndStatus->SetWindowText(lpszMessage);
  96. }
  97.  
  98. void CProgressDlg::OnCancel()
  99. {
  100. }
  101.  
  102. void CProgressDlg::SetRange(int nLower,int nUpper)
  103. {
  104.     m_nLower = nLower;
  105.     m_nUpper = nUpper;
  106.     m_Progress.SetRange(nLower,nUpper);
  107. }
  108.   
  109. int CProgressDlg::SetPos(int nPos)
  110. {
  111.     PumpMessages();
  112.     int iResult = m_Progress.SetPos(nPos);
  113.     UpdatePercent(nPos);
  114.     return iResult;
  115. }
  116.  
  117. int CProgressDlg::SetStep(int nStep)
  118. {
  119.     m_nStep = nStep; // Store for later use in calculating percentage
  120.     return m_Progress.SetStep(nStep);
  121. }
  122.  
  123. int CProgressDlg::OffsetPos(int nPos)
  124. {
  125.     PumpMessages();
  126.     int iResult = m_Progress.OffsetPos(nPos);
  127.     UpdatePercent(iResult+nPos);
  128.     return iResult;
  129. }
  130.  
  131. int CProgressDlg::StepIt()
  132. {
  133.     PumpMessages();
  134.     int iResult = m_Progress.StepIt();
  135.     UpdatePercent(iResult+m_nStep);
  136.     return iResult;
  137. }
  138.  
  139. void CProgressDlg::PumpMessages()
  140. {
  141.     // Must call Create() before using the dialog
  142.     ASSERT(m_hWnd!=NULL);
  143.  
  144.     MSG msg;
  145.     // Handle dialog messages
  146.     while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  147.     {
  148.       if(!IsDialogMessage(&msg))
  149.       {
  150.         TranslateMessage(&msg);
  151.         DispatchMessage(&msg);  
  152.       }
  153.     }
  154. }
  155.  
  156.  
  157. void CProgressDlg::UpdatePercent(int nNewPos)
  158. {
  159.     CWnd *pWndPercent = GetDlgItem(CG_IDC_PROGDLG_PERCENT);
  160.     int nPercent;
  161.     
  162.     int nDivisor = m_nUpper - m_nLower;
  163.     ASSERT(nDivisor>0);  // m_nLower should be smaller than m_nUpper
  164.  
  165.     int nDividend = (nNewPos - m_nLower);
  166.     ASSERT(nDividend>=0);   // Current position should be greater than m_nLower
  167.  
  168.     nPercent = nDividend * 100 / nDivisor;
  169.  
  170.     // Since the Progress Control wraps, we will wrap the percentage
  171.     // along with it. However, don't reset 100% back to 0%
  172.     if(nPercent!=100)
  173.       nPercent %= 100;
  174.  
  175.     // Display the percentage
  176.     CString strBuf;
  177.     strBuf.Format(_T("%d%c"),nPercent,_T('%'));
  178.  
  179.     CString strCur; // get current percentage
  180.     pWndPercent->GetWindowText(strCur);
  181.  
  182.     if (strCur != strBuf)
  183.         pWndPercent->SetWindowText(strBuf);
  184. }
  185.     
  186. /////////////////////////////////////////////////////////////////////////////
  187. // CProgressDlg message handlers
  188.  
  189. BOOL CProgressDlg::OnInitDialog() 
  190. {
  191.     CDialog::OnInitDialog();
  192.     m_Progress.SetRange(m_nLower,m_nUpper);
  193.     m_Progress.SetStep(m_nStep);
  194.     m_Progress.SetPos(m_nLower);
  195.  
  196.     CString strCaption;
  197.     VERIFY(strCaption.LoadString(m_nCaptionID));
  198.     SetWindowText(strCaption);
  199.  
  200.     VERIFY(m_Animation.Open(IDR_AVI_FILECOPY));
  201.     return TRUE;  
  202. }
  203.